package com.eric.service.impl;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eric.domain.dto.ProjectInfoDto;
import com.eric.domain.po.ProjectInfo;
import com.eric.mapper.ProjectInfoMapper;
import com.eric.service.IProjectInfoService;
import com.eric.utils.RegUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import java.util.List;

import static com.eric.utils.Constant.PAGE_CURRENT;
import static com.eric.utils.Constant.PAGE_SIZE;

/**
 * <p>
 * 项目信息表 服务实现类
 * </p>
 *
 * @author 新哥
 * @since 2024-03-12
 */
@Service
public class ProjectInfoServiceImpl extends MPJBaseServiceImpl<ProjectInfoMapper, ProjectInfo> implements IProjectInfoService {

    @Override
    @Transactional
//    插入一条项目信息
    public SaResult saveOneProjectService(ProjectInfoDto projectInfoDtoIn) {

        if (BeanUtil.isEmpty(projectInfoDtoIn)){
            return SaResult.error("对象为空，请核实后重新输入");
        }
        ProjectInfo projectInDb = query().eq("name", projectInfoDtoIn.getName()).one();
        if (!BeanUtil.isEmpty(projectInDb)){
            return SaResult.error("项目已存在！");
        }
        ProjectInfoDto projectInfoDto = chedkedProjectInfoDto(projectInfoDtoIn);


        if (StrUtil.isBlank(projectInfoDto.getName())){
            return SaResult.error("项目名称不能为空");
        }
        if (projectInfoDto.getName()!=null){
//            boolean nameMach = ReUtil.isMatch(RegUtil.realNameRegx, projectInfoDto.getName());
            boolean nameMach=true;
            if (!nameMach){
                return SaResult.error("名字格式不符合规定");
            }
        }

        if (StrUtil.isBlank(projectInfoDto.getContact())){
            return SaResult.error("项目联系人不能为空");
        }
        if (projectInfoDto.getContact()!=null){
            boolean contactMach = ReUtil.isMatch(RegUtil.realNameRegx, projectInfoDto.getContact());
            if (!contactMach){
                return SaResult.error("名字格式不符合规定");
            }
        }

        if (StrUtil.isBlank(projectInfoDto.getPhone())){
            return SaResult.error("电话不能为空");
        }
        if (projectInfoDto.getPhone()!=null){
            boolean phoneMach = ReUtil.isMatch(RegUtil.phoneRegex, projectInfoDto.getPhone());
            if (!phoneMach){
                return SaResult.error("电话号码格式不符合规定");
            }
        }

        if (StrUtil.isBlank(projectInfoDto.getAddress())){
            return SaResult.error("项目地址不能为空");
        }
        if (projectInfoDto.getAddress()!=null){
            boolean addressMach = ReUtil.isMatch(RegUtil.realNameRegx, projectInfoDto.getAddress());
            if (!addressMach){
                return SaResult.error("文字格式不符合要求");
            }
        }

        System.out.println(projectInfoDto);

        ProjectInfo projectinfo = BeanUtil.copyProperties(projectInfoDto, ProjectInfo.class);
        boolean save = save(projectinfo);

        return save?SaResult.ok("succeed"):SaResult.error("wrong");
    }

    @Override
//    删除有一条项目信息
    @Transactional
    @DeleteMapping("/{id}")
    public SaResult deleteOneProjectService(@PathVariable("id") Integer id) {
        boolean b = removeById(id);
        return b?SaResult.ok("succeed"):SaResult.error("wrong");
    }

    @Override
//    修改一条信息,这里需要有id值，否则会出问题
//    这个id，需要在前端获取到一条ProjectInfo包含有id的信息
//    这里的思路是，前端获取已存在的数据，同时这个数据是包含了id项的，通过id项，就可以修改数据了
    @Transactional
    @PutMapping("/modifyOne")
    public SaResult modifyOneProject(ProjectInfoDto projectInfoDtoIn) {

        if (BeanUtil.isEmpty(projectInfoDtoIn)){
            return SaResult.error("数据为空，请重新输入");
        }
        if (BeanUtil.isEmpty(projectInfoDtoIn.getId())){
            return SaResult.error("id数据为空，请重新输入");
        }
//        把两头多余的空字符去掉
        ProjectInfoDto projectinfoDto = chedkedProjectInfoDto(projectInfoDtoIn);

        if (projectinfoDto.getName()!=null){
            boolean nameMach = ReUtil.isMatch(RegUtil.realNameRegx, projectinfoDto.getName());
            if (!nameMach){
                return SaResult.error("名字格式不符合规定");
            }
        }
        if (projectinfoDto.getContact()!=null){
            boolean contactMach = ReUtil.isMatch(RegUtil.realNameRegx, projectinfoDto.getContact());
            if (!contactMach){
                return SaResult.error("名字格式不符合规定");
            }
        }
        if (projectinfoDto.getPhone()!=null){
            boolean phoneMach = ReUtil.isMatch(RegUtil.phoneRegex, projectinfoDto.getPhone());
            if (!phoneMach){
                return SaResult.error("电话号码格式不符合规定");
            }
        }
        if (projectinfoDto.getAddress()!=null){
            boolean addressMach = ReUtil.isMatch(RegUtil.realNameRegx, projectinfoDto.getAddress());
            if (!addressMach){
                return SaResult.error("文字格式不符合要求");
            }
        }

//        把ProjectInfoDto转换为ProjectInfo类型
        ProjectInfo projectinfo = BeanUtil.copyProperties(projectinfoDto, ProjectInfo.class);
        boolean b = updateById(projectinfo);
        return b?SaResult.ok("succeed"):SaResult.error("wrong");
    }


    @Override
    public SaResult getAllService(Integer cur,Integer pageSize) {
        if (cur==null){
            cur=PAGE_CURRENT;
        }

        if (pageSize==null){
            pageSize=PAGE_SIZE;
        }
        Page<ProjectInfo> pages = page(new Page<>(cur, pageSize));
        List<ProjectInfo> records = pages.getRecords();
        List<ProjectInfoDto> projectInfoDtoList = BeanUtil.copyToList(records, ProjectInfoDto.class);
        return SaResult.data(projectInfoDtoList);
    }
    @Override
    public SaResult getOneById(Integer id) {
        ProjectInfo byId = getById(id);
        ProjectInfoDto projectinfoDto = BeanUtil.copyProperties(byId, ProjectInfoDto.class);
        return SaResult.data(projectinfoDto);
    }

    @Override
    public SaResult getByNameService(String name,Integer pageCount,Integer pageSize) {
        String nameTrim = StrUtil.trim(name);
        if (StrUtil.isBlank(nameTrim)){
            return SaResult.error("输入数据为空");
        }
        if (pageCount==null){
            pageCount=PAGE_CURRENT;
        }
        if (pageSize==null){
            pageSize=PAGE_SIZE;
        }

        Page<ProjectInfo>page=new Page<>(pageCount,pageSize);
        List<ProjectInfo> projectInfoList = query().like("name", name).page(page).getRecords();
        List<ProjectInfoDto> projectInfoDtoList = BeanUtil.copyToList(projectInfoList, ProjectInfoDto.class);
        return SaResult.data(projectInfoDtoList);
    }

    @Override
    public SaResult getByContactService(String contact,Integer pageCount,Integer pageSize) {
        if (pageCount==null){
            pageCount=PAGE_CURRENT;
        }
        if (pageSize==null){
            pageSize=PAGE_SIZE;
        }
        String contactTrim = StrUtil.trim(contact);
        if (StrUtil.isBlank(contactTrim)){
            return SaResult.error("输入数据为空");
        }
        Page<ProjectInfo>page=new Page<>(pageCount,pageSize);
        List<ProjectInfo> projectInfoList = query().like("contact", contact).page(page).getRecords();
        List<ProjectInfoDto> projectInfoDtoList = BeanUtil.copyToList(projectInfoList, ProjectInfoDto.class);
        return SaResult.data(projectInfoDtoList);
    }

    @Override
    public SaResult getByAddressService(String address,Integer pageCount,Integer pageSize) {
        if (pageCount == null) {
            pageCount = PAGE_CURRENT;
        }
        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        String addressTrim = StrUtil.trim(address);
        if (StrUtil.isBlank(addressTrim)) {
            return SaResult.error("输入数据为空");
        }
        Page<ProjectInfo> page = new Page<>(pageCount, pageSize);
        List<ProjectInfo> projectInfoList = query().like("address", address).page(page).getRecords();
        List<ProjectInfoDto> projectInfoDtoList = BeanUtil.copyToList(projectInfoList, ProjectInfoDto.class);
        return SaResult.data(projectInfoDtoList);
    }

    //    检测字符串数据，如果是""就设成null，方便map不做修改
    private ProjectInfoDto chedkedProjectInfoDto(ProjectInfoDto projectinfoDto){
        String name =StrUtil.trim(projectinfoDto.getName()) ;
        if (StrUtil.isBlank(name)){
            projectinfoDto.setName(null);
        }
        else {
            projectinfoDto.setName(name);
        }

        String contact = StrUtil.trim(projectinfoDto.getContact());
        if (StrUtil.isBlank(contact)){
            projectinfoDto.setContact(null);
        }
        else {
            projectinfoDto.setContact(contact);
        }

        String phone = StrUtil.trim(projectinfoDto.getPhone());
        if (StrUtil.isBlank(phone)){
            projectinfoDto.setPhone(null);
        }
        else {
            projectinfoDto.setPhone(phone);
        }

        String address = StrUtil.trim(projectinfoDto.getAddress());
        if (StrUtil.isBlank(address)){
            projectinfoDto.setAddress(null);
        }
        else {
            projectinfoDto.setAddress(address);
        }

        String description = StrUtil.trim(projectinfoDto.getDescription());
        if (StrUtil.isBlank(description)){
            projectinfoDto.setDescription(null);
        }


        return projectinfoDto;
    }




}
